import web
from cacheout import Cache

from py_wechat_tools import check
from wechat_tools_test import oa

# --- 启动服务 ---

urls = (
    '/', 'index',
    '/index', 'index',
)


# class index:
#     """ 用于微信服务器验证url有效性 """
#
#     def GET(self):
#         arg = web.input()
#         res = check.message_push_check(
#             signature=arg.signature,
#             timestamp=arg.timestamp,
#             nonce=arg.nonce,
#             token="123",
#         )
#
#         if not res:
#             return '验证不通过！'
#         return arg.echostr


class index:
    def GET(self):
        # 接收参数
        arg = web.input()

        # 用户授权只需要传递几个参数，
        url, user_info = oa.authorize_userinfo(
            code=arg.get('code'),
            # redirect_uri='回调地址'  # 如果初始化是未传入这个参数必填
        )

        # url不为空时，重定向到url
        if url:
            return web.seeother(url)

        return user_info



if __name__ == "__main__":

    cache = Cache()
    app = web.application(urls, globals())
    app.run()